import {defineStore} from "pinia";
import routerLinks from "src/router/routerLinks";

export const useFunctionsStore = defineStore("functions", {
  state: () => ({
    role: "",
    userInfo: "",
    osRole: 0,
    mode: "server",
    scene: "",
    remoteMapId: "",
    keyboardMove: false, // 是否键盘移动
    storageNumber: 8, // 自身库多少
    isIP: true, // 是否显示抬头IP
    menuList: [], // 全部的路由
    mobileLinks: [], // 移动端路由
    deviceLinks: [], // 机器人端路由
    serverLinks: [], // 中控端路由
    isHiddenSecondaryPosition: false, // 是否隐藏副点
  }),
  getters: {},
  actions: {
    // 设置配置文件ip
    setParams(data) {
      // linksMap 对config文件中出现的deviceLinks,serverLinks进行收集
      for (const key in data) {
        this[key] = data[key];
      }
      this.setRoutes();
    },
    setRoutes() {
      const linksMap = new Map([
        ["device", Array.isArray(this.deviceLinks) ? this.deviceLinks : []],
        ["server", Array.isArray(this.serverLinks ? this.serverLinks : [])],
      ]);
      // config 文件中出现hiddenLinks字段进行路由判断
      const links = linksMap.get(this.mode) || []; // 获取页面想展示的路由集合
      this.menuList = routerLinks().map((item) => {
        // 通过isHidden字段判断是否显示 active判断是否激活快捷路由
        // 历史记录里面是否存在于全局路由中来处理isHidden和active
        const itemInMenu =
          Array.isArray(this.menuList) &&
          this.menuList.length > 0 &&
          this.menuList.find((menu) => menu.link === item.link);
        item.active = itemInMenu?.active || false; // 是否激活快捷路由
        item.isHidden = links.length > 0 && !links.includes(item.link);
        return item;
      });
    },
  },
  // 开启数据缓存
  persist: {
    enabled: true,
    strategies: [
      {
        key: "functions",
        storage: localStorage,
        paths: [
          "role",
          "userInfo",
          "mode",
          "osRole",
          "scene",
          "storageNumber",
          "keyboardMove",
          "isIP",
          "menuList",
          "isHiddenSecondaryPosition"
        ],
      },
    ],
  },
});
